﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;

namespace YXShop.Web
{
    public partial class showVote : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (ChangeHope.WebPage.PageRequest.GetFormString("Option") != string.Empty && ChangeHope.WebPage.PageRequest.GetFormString("x") != string.Empty)
            {
                string types = Request.Form["Option"].Trim();
                int strid = ChangeHope.WebPage.PageRequest.GetFormInt("strid");
                string x = ChangeHope.WebPage.PageRequest.GetFormString("x");
                if (types == "voteTphd")
                {
                    UpdateVoteTphd(strid, x);
                }
                else if (types == "voteTpdx")
                {
                    UpdateVoteTpdx(strid, x);
                }
                else if (types == "voteTptj")
                {
                    UpdateVoteTptj(strid, x);
                }
                else if (types == "voteTpsf")
                {
                    UpdateVoteTptj(strid, x);
                }
                Response.End();
                return;
            }
            else
            {
                Response.Write(GetVoteResult(ChangeHope.WebPage.PageRequest.GetQueryInt("id")));
            }
        }

        #region 查看调查
        protected string GetVoteResult(int strid)
        {
            YXShop.BLL.Accessories.Question bll = new YXShop.BLL.Accessories.Question();
            string content = bll.GetModelById(strid).QuestionDiscription;
            string[] arr = content.Split('|');
            decimal decNum = 0;
            StringBuilder htmlBody = new StringBuilder();
            foreach (string i in arr)
            {
                decNum = Convert.ToDecimal(i.Split(',')[1]) + decNum;
            }
            htmlBody.Append("<table width=100% border=1 cellpadding=0 cellspacing=0 text-align: center>\n");
            foreach (string j in arr)
            {
                string itemName = j.Split(',')[0].ToString();//得到选项名字
                decimal itemNum = Convert.ToDecimal(j.Split(',')[1]);
                if (decNum == 0)
                {
                    decNum++;
                }
                decimal decPercent = (itemNum / decNum) * 100;
                string strPercent = decPercent.ToString(); //将百分比转为字符型
                if (strPercent.Length > 5) //如果百分比结果长度超过5位
                {
                    strPercent = strPercent.Substring(0, 5);//将百分比的余数截短为"00.00"
                }
                htmlBody.Append("<tr><td width=100 style=font-size:9pt;text-align:center>" + itemName + "</td><td width=50 style= font-size: 9pt; color=red;text-align:center>" + itemNum.ToString() + "票</td><td style=font-size:9pt><img src=\"images/1_11.jpg\"  height=10 width=" + strPercent + "%>" + strPercent + "%</td></tr>\n");
            }
            return htmlBody.ToString();
        }
        #endregion

        #region ajax投票

        protected void UpdateVoteTphd(int strid, string wz)
        {
            string aasd = wz.Trim();
            string kk = string.Empty;
            if (aasd != string.Empty || aasd != string.Empty)
            {
                YXShop.BLL.Accessories.Question bll = new YXShop.BLL.Accessories.Question();
                YXShop.Model.Accessories.Question model = bll.GetModelById(strid);
                string cr = string.Empty;
                if (model.QuestionDiscription==string.Empty)
                {
                    cr = wz;
                }
                else
                {
                    cr = model.QuestionDiscription + "|" + wz;
                }
                model.QuestionDiscription = cr;
                bll.Update(model);
                Response.Write("ok");
            }
            else
            {
                Response.Write("err");
            }
        }

        protected void UpdateVoteTpdx(int strid, string wz)
        {
            YXShop.BLL.Accessories.Question bll = new YXShop.BLL.Accessories.Question();
            YXShop.Model.Accessories.Question model = bll.GetModelById(strid);
            string[] arrContent = model.QuestionDiscription.Split('|');
            string[] dxd=wz.Split(',');
            string cr = string.Empty;
            for (int i = 0; i < arrContent.Length; i++)
            {
                string cr1 = arrContent[i];
                for(int j=0;j<dxd.Length;j++)
                {
                    if (Convert.ToInt32(dxd[j].ToString()) - 1 == i)
                    {
                        int gg = Convert.ToInt32(arrContent[i].ToString().Split(',')[1].ToString()) + 1;

                        cr1 = arrContent[i].ToString().Split(',')[0].ToString() + "," + gg;

                    }
                }
                if (cr == string.Empty)
                {
                    cr = cr1;
                }
                else
                {
                    cr = cr + "|" + cr1;
                }
            }
            model.QuestionDiscription = cr;
            bll.Update(model);
            Response.Write("ok");
        }

        protected void UpdateVoteTptj(int strid, string wz)
        {
            YXShop.BLL.Accessories.Question bll = new YXShop.BLL.Accessories.Question();
            YXShop.Model.Accessories.Question model = bll.GetModelById(strid);
            string[] arrContent = model.QuestionDiscription.Split('|');
            string[] dxd = wz.Split(',');
            string cr = string.Empty;
            for (int i = 0; i < arrContent.Length; i++)
            {
                string cr1 = arrContent[i];
                for (int j = 0; j < dxd.Length; j++)
                {
                    if (Convert.ToInt32(dxd[j].ToString()) - 1 == i)
                    {
                        int gg = Convert.ToInt32(arrContent[i].ToString().Split(',')[1].ToString()) + 1;

                        cr1 = arrContent[i].ToString().Split(',')[0].ToString() + "," + gg;

                    }
                }
                if (cr == string.Empty)
                {
                    cr = cr1;
                }
                else
                {
                    cr = cr + "|" + cr1;
                }
            }
            model.QuestionDiscription = cr;
            bll.Update(model);
            Response.Write("ok");
        }
        #endregion
    }
}
